package club.weiyuanstudio.android.app.message_station.ui.home

import android.annotation.SuppressLint
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import club.weiyuanstudio.android.app.message_station.R
import club.weiyuanstudio.android.app.message_station.api.data.Node

/**
 * [RecyclerView.Adapter] that can display a [DummyItem].
 * TODO: Replace the implementation with code for your data type.
 */
class NodeRecyclerViewAdapter(
    private val values: Array<Node>
) : RecyclerView.Adapter<NodeRecyclerViewAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.node_card_item, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = values[position]
        with(holder) {
            deviceName.text = item.name
            networkType.text = "Network: ${item.networkType}"
            batteryLevel.text = "Battery: ${item.battery}%"
            nodeUUID.text = item.uuid
            lastUpdate.text = item.lastUpdate.toString()
        }
    }

    override fun getItemCount(): Int = values.size

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val deviceName = view.findViewById(R.id.textViewDeviceName) as TextView
        val networkType = view.findViewById(R.id.textViewNetWorkType) as TextView
        val batteryLevel = view.findViewById(R.id.textViewBatteryLevel) as TextView
        val nodeUUID = view.findViewById(R.id.textViewNodeUUID) as TextView
        val lastUpdate = view.findViewById(R.id.textViewLastUpdate) as TextView
    }
}
